<template>
  <px-dialog v-model="visible" class="equip-dialog-wrap" title="选择备件" width="1000px" @open="openDialog" @close="closeDialog">
    <div class="equip-dialog-content">
      <px-form ref="formRef" class="custom-form-ui equip-form" :model="queryParams" label-position="right" label-width="120px">
        <px-row :gutter="16">
          <px-col :span="7">
            <px-form-item label="备件名称">
              <px-input
                v-model="queryParams.spareName"
                placeholder="请输入备件名称"
                maxlength="50"
                clearable
                @blur="queryParams.spareName = $event.target.value.trim()"
              />
            </px-form-item>
          </px-col>
          <px-col :span="7">
            <px-form-item label="归属地块">
              <px-select v-model="queryParams.facCode" placeholder="请选择归属地块" disabled>
                <px-option v-for="item in facCodeList" :key="item.id" :label="item.name" :value="item.value" />
              </px-select>
            </px-form-item>
          </px-col>
          <px-col :span="7">
            <px-button type="primary" @click="queryTable">查询</px-button>
            <px-button @click="resetTable">重置</px-button>
          </px-col>
        </px-row>
      </px-form>
      <px-table :data="tableData" class="equip-table" size="small" max-height="320" border @selection-change="selectionChange">
        <px-table-column type="selection" min-width="40" align="center" fixed="left" />
        <px-table-column
          v-for="(item, index) in tableColumns"
          :key="index"
          :label="item.label"
          :min-width="item.minWidth"
          :align="item.align"
          :show-overflow-tooltip="item.showOverflowTooltip"
          :fixed="item.fixed"
        >
          <template #default="scope">
            {{ scope.row[item.prop] === 0 ? scope.row[item.prop] : scope.row[item.prop] ? scope.row[item.prop] : "-" }}
          </template>
        </px-table-column>
      </px-table>
      <pagination
        v-show="total > 0"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        :total="total"
        @pagination="queryTable"
      />
      <div style="margin-top: 16px; text-align: right">
        <px-button @click="closeDialog">取消</px-button>
        <px-button type="primary" @click="handleSubmit">确定</px-button>
      </div>
    </div>
  </px-dialog>
</template>
<script setup lang="ts">
import { queryDictData } from "@/api/common";
import { querySparePartsLedger } from "@/api/erc/spareParts";
import { computed, reactive, toRefs } from "vue";

// 调用父级-关闭弹窗方法
const emits = defineEmits(["closeDialog", "addTableRow"]);
// 父级传过来的值
const props = defineProps(["visible", "facCodeData"]);
const visible = computed(() => props.visible);
// 页面本身的值
const state = reactive<any>({
  facCodeList: [],
  queryParams: {
    spareName: "",
    facCode: "",
    pageNum: 1,
    pageSize: 10
  },
  tableColumns: [
    { prop: "facCode", label: "归属地块", minWidth: "100", showOverflowTooltip: true, fixed: "left" },
    { prop: "sparePartName", label: "备件名称", minWidth: "180", showOverflowTooltip: true },
    { prop: "eqpTypeCodeDesc", label: "备件类型", minWidth: "180", showOverflowTooltip: true },
    { prop: "specification", label: "规格型号", minWidth: "150", showOverflowTooltip: true },
    { prop: "brandVal", label: "品牌", minWidth: "80", showOverflowTooltip: true },
    { prop: "sparePartUnit", label: "单位", minWidth: "80", showOverflowTooltip: true },
    { prop: "currentStock", label: "库存数量", minWidth: "80", showOverflowTooltip: true }
  ],
  tableData: [],
  total: 0,
  selectRows: []
});
const { facCodeList, queryParams, tableColumns, tableData, total, selectRows } = toRefs(state);

// 查询备件列表
const queryTable = async () => {
  const params = queryParams.value;
  const data = await querySparePartsLedger(params);
  tableData.value = data?.data;
  if (tableData.value && tableData.value.length > 0) {
    tableData.value.forEach(item => {
      item.quantity = 0;
    });
  }
  total.value = data?.total;
};
// 重置筛选条件和备件列表
const resetTable = async () => {
  queryParams.value.spareName = "";
  queryTable();
};
// 表格多选框选中
const selectionChange = (val: any) => {
  selectRows.value = [];
  val.forEach((item: any) => {
    if (item.id) {
      selectRows.value.push(item);
    }
  });
};
// 确定选择备件
const handleSubmit = () => {
  emits("closeDialog");
  emits("addTableRow", selectRows.value);
};

// 打开弹窗
const openDialog = async () => {
  facCodeList.value = await queryDictData({ typeCode: "ERC_FACTORY_AREA" });
  queryParams.value = {
    spareName: "",
    facCode: props.facCodeData || "",
    pageNum: 1,
    pageSize: 20
  };
  queryTable();
};
// 关闭弹窗
const closeDialog = () => {
  emits("closeDialog");
};
</script>
<style lang="scss" scoped>
@import url("@/views/erc/scss/add-dialog.scss");

.px-pagination {
  right: 32px;
}
</style>
